import * as gulp from 'gulp';
import * as gulpLoadPlugins from 'gulp-load-plugins';
import {join, sep, normalize} from 'path';
var gulpif = require('gulp-if');
import Config from '../../config';
import {TemplateLocalsBuilder} from '../../utils';

const plugins = <any>gulpLoadPlugins();

/**
 * Executes the build process, injecting the JavaScript and CSS dependencies into the `index.html` for the production
 * environment.
 */
export = () => {
    return gulp.src(join(Config.CLIENT_HTML_SRC, 'home.html'))
        .pipe(plugins.useref({
            searchPath: [".", "./src/public",Config.SERVER_PUBLIC_DEST],
            base: "../"
        }))
        .pipe(gulpif('*.css', plugins.minifyCss()))
        .pipe(gulpif('*.js', plugins.uglify()))
        .pipe(gulpif(['**/*.js', '**/*.css'], plugins.rev()))
        .pipe(plugins.revReplace())
        .pipe(gulpif(['**/*.js', '**/*.css'], gulp.dest(Config.SERVER_PUBLIC_DEST)))
        .pipe(gulpif('*.html', plugins.template(new TemplateLocalsBuilder().wihtoutStringifiedEnvConfig().build())))
        .pipe(gulpif('*.html', gulp.dest(Config.CLIENT_HTML_DEST)));
};
